package api

import (
	"day12_30/internal/api/request"
	"day12_30/internal/api/response"
	"day12_30/internal/models/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func CreatePrice(c *gin.Context) {
	var json request.Price
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")

	s := value.(string)

	userId, _ := strconv.Atoi(s)

	price := service.CreatePrice(userId, json)

	if price != "" {
		response.Response(c, 400, price, nil)
	}

	response.Response(c, 200, "估价成功", nil)

}

func FindPrice(c *gin.Context) {
	var json request.FindPrice
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	price, s := service.FindPrice(&json)
	if s != "" {
		response.Response(c, 400, s, nil)
	}
	response.Response(c, 200, "查询成功", price)
}
